package com.boarsoft.message.sofamq.converter;

import com.alibaba.fastjson.JSON;
import com.boarsoft.message.bean.Message;
import com.boarsoft.serialize.JavaSerializer;
import com.boarsoft.serialize.ObjectSerializer;

import java.nio.charset.StandardCharsets;

public class DefaultSofaMsgConverter implements SofaMsgConverter {

	protected ObjectSerializer serializer = new JavaSerializer();

	@Override
	public io.openmessaging.api.Message convert(Message rm) {
		io.openmessaging.api.Message message = new io.openmessaging.api.Message(rm.getTarget(), rm.getTag(), rm.getKey(),
				JSON.toJSONString(rm.getContent()).getBytes(StandardCharsets.UTF_8));
		message.setMsgID(rm.getId());
		return message;
	}

	@Override
	public Message convert(io.openmessaging.api.Message m) {
		Message message = new Message();
		message.setId(m.getMsgID());
		message.setTarget(m.getTopic());
		message.setContent(JSON.parse(m.getBody()));
		message.setTag(m.getTag());
		return message;
	}

	public ObjectSerializer getSerializer() {
		return serializer;
	}

	public void setSerializer(ObjectSerializer serializer) {
		this.serializer = serializer;
	}
}
